<template>
  <zero-dialog :showDialog.sync="addUpdateFormVisible"
               :fullscreen.sync="fullscreen"
               :btnExist="btnExist"
               :loading="loading"
               :title="title"
               :submit="addOrupdate">
    <div slot="body"
         style="height:450px">
      <el-form :model="addUpdateForm"
               :rules="rules"
               label-width="80px"
               size="medium"
               ref="addUpdateFormRef">

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item prop="name"
                          label="角色名称">
              <el-input v-model.trim="addUpdateForm.name"
                        placeholder="请输入角色名称" />
            </el-form-item>
            <el-form-item prop="state"
                          label="角色状态">
              <el-tooltip :content="addUpdateForm.state==='ENABLE'?'点击禁用':'点击启用'"
                          placement="top">
                <el-switch v-model="addUpdateForm.state"
                           active-value="ENABLE"
                           inactive-value="DISABLE">
                </el-switch>
              </el-tooltip>
            </el-form-item>
            <el-form-item label="备注">
              <el-input type="textarea"
                        v-model="addUpdateForm.remarks"
                        placeholder="请输入备注" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-card class="box-card">
              <div slot="header"
                   class="clearfix">
                <span>权限信息</span>
              </div>
              <PowerTree ref="treeRef"
                         :powerArray="powerArray" />
            </el-card>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </zero-dialog>
</template>

<script>

import PowerTree from './PowerTree' // 权限树

export default {
  name: 'AddUpdateForm',
  components: {
    PowerTree
  },
  props: {
    addUrl: String,
    updateUrl: String,
    getDataList: {
      type: Function,
      default: null
    }
  },
  data () {
    return {
      loading: false,
      fullscreen: false, // 弹框全屏
      btnExist: true, // 按钮是否存在
      addUpdateFormVisible: false, // 是否显示弹框
      title: '', // 弹框标题
      addUpdateForm: {},
      powerArray: [],
      initFrom: {
        state: 'ENABLE'
      },
      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        state: [{ required: true, message: '请选择角色状态', trigger: 'blur' }]
      }
    }
  },
  created () { },
  methods: {
    initDialog (row) {
      if (row) {
        const config = {
          url: '/sysPower/getPowerIdsByRoleId',
          method: 'get',
          params: {
            roleId: row.id
          }
        }
        this.$axios(config)
          .then(res => {
            this.addUpdateFormVisible = true
            this.addUpdateFormReset()
            this.title = '修改角色'
            this.addUpdateForm = JSON.parse(JSON.stringify(row))
            this.powerArray = res.data
          })
      } else {
        this.addUpdateFormVisible = true
        this.addUpdateFormReset()
        this.powerArray = []
        this.title = '新增角色'
      }
    },
    addOrupdate () {
      // 添加或修改
      this.$refs.addUpdateFormRef.validate(valid => {
        if (valid) {
          this.loading = true
          this.btnExist = false
          // 处理权限
          this.addUpdateForm.powers = this.$refs.treeRef.getCheckedKeys().map(it => {
            return { id: it }
          })
          const config = {
            url: this.addUpdateForm.id ? this.updateUrl : this.addUrl,
            method: 'post',
            data: this.addUpdateForm
          }
          this.$axios(config)
            .then(res => {
              this.loading = false
              this.addUpdateFormVisible = false
              this.btnExist = true
              this.getDataList()
              this.$message.success(res.msg)
            })
            .catch(() => {
              this.loading = false
              this.btnExist = true
            })
        }
      })
    },
    addUpdateFormReset () {
      // 重置表单 需要转成字符串赋值，不然这两个对象的值会一直相等
      this.addUpdateForm = JSON.parse(JSON.stringify(this.initFrom))
      this.$nextTick(() => {
        this.$refs.treeRef.filterText = ''
        this.fullscreen = false
        this.$refs.treeRef.getPowerTree() // 权限初始化
        this.$refs.addUpdateFormRef.clearValidate()
      })
    }
  }
}
</script>
